package cn.xuqiudong.common.blog.search.model;

import cn.xuqiudong.common.blog.model.BlogArticle;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.io.Serializable;

/**
 * 说明 :  博客存入es中的实体
 * @author Vic.xu
 * @since  2020/9/16 0016 11:28
 */
public class SearchBlogArticleModel implements Serializable {


    private static final long serialVersionUID = 1L;

    private String id;
    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * 作者
     */
    private String author;

    /**
     * 分类
     */
    private String categoryName;

    /**
     * 专题
     */
    private String topicName;

    /**
     * 多标签
     */
    private String tagNames;

    /**
     * 图标
     */
    private String iconUrl;


    private String createTime;

    public SearchBlogArticleModel() {
    }

    public SearchBlogArticleModel(BlogArticle blogArticle) {
        this.id = String.valueOf(blogArticle.getId());
        this.title = blogArticle.getTitle();
        this.content = blogArticle.getContent();
        this.author = blogArticle.getAuthor();
        this.categoryName = blogArticle.getCategoryName();
        this.topicName = blogArticle.getTopicName();
        this.tagNames = blogArticle.getTagNames();
        this.iconUrl = blogArticle.getIconUrl();
        this.createTime = DateFormatUtils.format( blogArticle.getCreateTime(), "yyyy-MM-dd HH:mm:ss");

    }

    /**summary显示最大长度*/
    public static final int SUMMARY_COUNT = 400;

    /**
     *给列表页使用的
     */
    public String getSummary() {
        if (StringUtils.isNotBlank(content) && content.length() > SUMMARY_COUNT) {
            return reduceContent(content);
        }
        return content;
    }

    public static String reduceContent(String content){
        String delimiter = "<strong class='highlight'>";
        // 这里简单个根据分割 判断下出现的次数
        String[] split = content.split(delimiter);
        //当超过2次的时候 只保留前两个
        if (split.length < 3) {
            return content;
        }

        int firstIndex = content.indexOf(delimiter);

        if (firstIndex == -1) {
            // 如果找不到第一个分隔符，返回原字符串
            return content;
        }

        int secondIndex = content.indexOf(delimiter, firstIndex + delimiter.length());
        if (secondIndex == -1) {
            // 如果找不到第二个分隔符，返回包含第一个分隔符的字符串
            return content;
        }
        int thirdIndex = content.indexOf(delimiter, secondIndex + delimiter.length());
        if (thirdIndex == -1) {
            // 如果找不到第三个分隔符，返回包含第一个分隔符的字符串
            return content;
        }

        // 截取从开头到第三个分隔符位置的字符串（不包括第三个分隔符）
        return content.substring(0, thirdIndex);
    }




    public static void main(String[] args) {
        String content = "…lookup);\n" +
                "\t\treturn datas;\n" +
                "\t}\n" +
                "\n" +
                "\tprivate List&lt;ColumnEntity&gt; queryColumns(<strong class='highlight'>String</strong> tableName) {\n" +
                "\t\treturn generatorMapper.queryColumns(tableName);\n" +
                "\t}\n" +
                "\n" +
                "\tprivate TableEntity queryTable(<strong class='highlight'>String</strong> tableName) {\n" +
                "\t\treturn generatorMapper.queryTable(tableName);\n" +
                "\t}\n" +
                "\n" +
                "\t/**\n" +
                "\t * 根据表名生成代码\n" +
                "\t * \n" +
                "\t * @param moduleName\n" +
                "\t * @param packageName\n" +
                "\t */\n" +
                "\tpublic byte[] generatorCode(<strong class='highlight'>String</strong>[] tableNames, <strong class='highlight'>String</strong> packageName…";
        System.out.println(reduceContent(content));

    }


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public String getTopicName() {
        return topicName;
    }

    public void setTopicName(String topicName) {
        this.topicName = topicName;
    }

    public String getTagNames() {
        return tagNames;
    }

    public void setTagNames(String tagNames) {
        this.tagNames = tagNames;
    }

    public String getIconUrl() {
        return iconUrl;
    }

    public void setIconUrl(String iconUrl) {
        this.iconUrl = iconUrl;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
